package dev.android.yeqh.alarmclock.adapters;

import java.util.Calendar;
import java.util.List;

import dev.android.yeqh.alarmclock.R;
import dev.android.yeqh.alarmclock.model.Alarm;
import dev.android.yeqh.alarmclock.model.AlarmType;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AlarmListAdapter extends BaseAdapter{
	private Context mContext;
	private List<Alarm> mAlarms;
	private int mLayout;
	
	public AlarmListAdapter(Context context, List<Alarm> alarms, int layout) {
		mContext = context;
		mAlarms = alarms;
		mLayout = layout;
	}

	public int getCount() {
		return mAlarms.size();
	}

	public Object getItem(int position) {
		return mAlarms.get(position);
	}

	public long getItemId(int position) {
		return mAlarms.get(position).getID();
	}
	
	public void removeItem(int position) {
		mAlarms.remove(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		
		view = ((Activity)mContext).getLayoutInflater().inflate(mLayout, null);
		TextView alarmTime = (TextView)view.findViewById(R.id.list_alarm_time);
		TextView alarmType = (TextView)view.findViewById(R.id.list_alarm_type);
		TextView alarmDescription = (TextView)view.findViewById(R.id.list_alarm_description);
		
		Alarm alarm = mAlarms.get(position);
		Calendar alarmCalendar = Calendar.getInstance();
		alarmCalendar.setTimeInMillis(alarm.getAlarmTime());
		alarmTime.setText(String.format("%02d", alarmCalendar.get(Calendar.HOUR_OF_DAY)) + ":" + 
				String.format("%02d", alarmCalendar.get(Calendar.MINUTE)));
		if (alarm.getRepeatType() == AlarmType.TYPE_SETTED_TIME) {
			alarmType.setText(alarm.getRepeatType().toString() + 
					String.format("%02d",(alarm.getRepeatTime()/(1000 * 60))/60) + ":" +
					String.format("%02d",(alarm.getRepeatTime()/(1000 * 60))%60));
		} else {
			alarmType.setText(alarm.getRepeatType().toString());
		}
		alarmDescription.setText(alarm.getAlarmDescription());
		
		return view;
	}
}
